<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JFormHelper::loadFieldClass('text');

class JFormFieldComponentImport extends JFormFieldText
{

    public $type = 'ComponentImport';

    protected function getInput()
    {
        // Initialize some field attributes.
        $sSize = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
        
        $sMaxLength = $this->element['maxlength'] ? ' maxlength="' . (int) $this->element['maxlength'] . '"' : '';
        
        $sClass = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
        
        $sReadonly = ' readonly="readonly"';
        
        $sDisabled = ' disabled="disabled"';
        
        // Initialize JavaScript field attributes.
        $sOnchange = $this->element['onchange'] ? ' onchange="' . (string) $this->element['onchange'] . '"' : '';
        
        // Change the display component title.
        $oDatabase = JFactory::getDbo();
        $oQuery = $oDatabase->getQuery(true);

        $oQuery->select('ac.title');
        $oQuery->from('#__auto_component AS ac');
        $oQuery->where('ac.id = ' . (int) $this->value);

        $oDatabase->setQuery($oQuery);

        try
        {
            $oComponent = $oDatabase->loadObject();
        }
        catch (RuntimeException $oException)
        {
            JError::raiseWarning(500, $oException->getMessage());
        }
        
        $sComponent = '';
        
        if ($oComponent)
            $sComponent = $oComponent->title;
        else
            $sComponent = JText::_('COM_AUTOGENERATOR_IMPORT_FIELD_COMPONENT_NOT_VALID');
        
        $sHtml = '<input type="text" name="' . $this->name . '" id="' . $this->id . '"' . ' value="' . htmlspecialchars($sComponent, ENT_COMPAT, 'UTF-8') . '"' . $sClass . $sSize . $sDisabled . $sReadonly . $sOnchange . $sMaxLength . '/>';
        $sHtml .= '<input type="hidden" name="' . $this->name . '" value="' . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '" />';
        
        return $sHtml;
    }

}
